/*
* Copyright 2011 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/
package org.etherframework.mediation
{
	import flash.errors.IllegalOperationError;
	import flash.events.IEventDispatcher;
	
	import mx.core.IFactory;
	
	import org.etherframework.common.mediation.IMediations;
	import org.etherframework.core.$Globals;
	import org.etherframework.core.GlobalConfig;
	import org.etherframework.core.bridge.IBridge;
	import org.etherframework.core.bridge.IBridgeFacades;
	import org.etherframework.core.common.IInstanceFactory;
	import org.etherframework.core.context.Context;
	import org.etherframework.core.module.IModulePart;
	import org.etherframework.core.module.IModulePartFacade;
	import org.etherframework.core.module.ModulePartFactory;
	import org.etherframework.mediation.core.MediationData;
	import org.etherframework.mediation.core.MediationsFacade;
	import org.etherframework.mediation.core.ProviderData;
	import org.etherframework.mediation.manager.IMediationManager;
	import org.etherframework.mediation.manager.MediationManager;
	import org.floxy.ProxyRepository;

	public class Mediations implements IMediations, IModulePart
	{
		private static var _config:GlobalConfig;
		private static var _mediationManager:IMediationManager;
		
		private var _owner:IMediations;
		private var _context:Context;
		
		public function Mediations(owner:IMediations, context:Context)
		{
			_owner = owner;
			_context = context;
		}
		
		public static function initialize():void
		{
			if ($Globals.config != _config)
			{
				_config = $Globals.config;
				
				var factory:IFactory = _config.superFactory.createFactory(MediationManager);
				_mediationManager = MediationManager(factory.newInstance());
				factory = _config.superFactory.createFactory(ProxyRepository);
				_mediationManager.initialize(ProxyRepository(factory.newInstance()), 
					new MediationData(), new ProviderData());
				
				$Globals.config.instanceFactory = _mediationManager as IInstanceFactory;
				$Globals.config.mediationFactory = new ModulePartFactory(Mediations);
			}
		}
		
		public function complete():void
		{
			throw new IllegalOperationError("Method is not implemented.");
		}
		
		public function init(bridge:IBridge):void
		{
			throw new IllegalOperationError("Method is not implemented.");
		}
		
		public function destroy():void
		{
			throw new IllegalOperationError("Method is not implemented.");
		}
		
		public function getFacade(owner:IBridgeFacades):IModulePartFacade
		{
			return new MediationsFacade(owner, this, _mediationManager);
		}
		
		public static function prepare(classes:Array, bunch:int = -1):IEventDispatcher
		{
			if (!_config)
				initialize();
			
			return _mediationManager.prepare(classes, bunch);
		}
	}
}